package org.example.blog.services;

import org.example.blog.entities.*;
import org.example.blog.services.impl.CommentServiceImpl;
import org.example.blog.services.impl.FavoriteServiceImpl;
import org.example.blog.services.impl.PostServiceImpl;
import org.example.blog.services.impl.UserServiceImpl;
import org.zoomdev.zoom.common.utils.Page;
import org.zoomdev.zoom.dao.Dao;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

public class BlogService {

    private final UserService userService;
    private final PostService postService = new PostServiceImpl();
    private final CommentService commentService = new CommentServiceImpl();
    private final FavoriteService favoriteService = new FavoriteServiceImpl();

    public BlogService(Dao dao) {
        userService = new UserServiceImpl(dao);
    }

    // 用户注册流程
    public User registerNewUser(String username, String email, String password) {
        return userService.registerUser(username, email, password);
    }

    // 文章发布流程
    public Post publishPost(String title, String content, Long authorId, Long categoryId) {
        User author = userService.getUserById(authorId);
        // 伪代码 - 获取分类
        Category category = new Category(); // categoryService.getCategoryById(categoryId);
        return postService.createPost(title, content, author, category);
    }

    // 添加评论流程
    public Comment addCommentToPost(Long postId, Long userId, String content) {
        Post post = postService.getPostById(postId);
        User user = userService.getUserById(userId);
        return commentService.createComment(post, user, content);
    }

    // 获取文章详情流程
    public BlogPostDetail getPostDetail(Long postId) {
        BlogPostDetail detail = new BlogPostDetail();
        detail.setPost(postService.getPostById(postId));
        detail.setComments(commentService.getCommentsByPostId(postId));
        return detail;
    }

    public List<Post> getPostList(int count) {
        return postService.getLatestPosts(100);
    }

    public void deletePost(Long id) {
        postService.deletePost(id);
    }


    // 用户登录流程
    public User userLogin(String email, String password) {
        return userService.login(email, password);
    }

    // 获取用户的文章列表
    public List<Post> getPostsByUser(Long userId) {
        // 伪代码 - 实际应用中应通过DAO查询
        return Arrays.asList();
    }

    // 获取最新文章
    public List<Post> getLatestPosts(int count) {
        return postService.getLatestPosts(count);
    }

    // 搜索文章
    public List<Post> searchPosts(String keyword) {
        return postService.searchPosts(keyword);
    }

    // 删除评论
    public void deleteComment(Long commentId) {
        // 伪代码 - 实际应用中应通过服务删除
        System.out.println("删除评论: " + commentId);
    }

    // 更新用户信息
    public User updateUser(User user) {
        user.setUpdatedAt(LocalDateTime.now());
        // 伪代码 - 更新数据库
        return user;
    }

    public Post updatePost(Post streamPost) {
        return postService.updatePost(streamPost);
    }

    /**
     * 收藏文章
     *
     * @param userId 用户ID
     * @param postId 文章ID
     * @return 收藏信息
     */
    public Favorite favoritePost(Long userId, Long postId) {
        return favoriteService.addFavorite(userId, postId);
    }

    /**
     * 取消收藏
     *
     * @param userId     用户ID（用于验证）
     * @param favoriteId 收藏ID
     */
    public void unfavoritePost(Long userId, Long favoriteId) {
        // 这里可以添加验证，确保该收藏记录属于该用户
        favoriteService.removeFavorite(favoriteId);
    }

    /**
     * 检查用户是否收藏了文章
     *
     * @param userId 用户ID
     * @param postId 文章ID
     * @return 是否收藏
     */
    public boolean isFavorite(Long userId, Long postId) {
        return favoriteService.isFavorite(userId, postId);
    }

    /**
     * 获取用户收藏的文章列表（分页）
     *
     * @param userId 用户ID
     * @param page   页码
     * @param size   每页数量
     * @return 分页的文章
     */
    public Page<Post> getUserFavorites(Long userId, int page, int size) {
        return favoriteService.getUserFavorites(userId, page, size);
    }
}

